﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using Distribution.General;


namespace Distribution.Data
{
    public static class Helper
    {
        #region - Public methods -

        /// <summary>
        /// Opens and returns a sql connection
        /// </summary>
        public static SqlCeConnection GetConnection()
        {
            return GetConnection(PPIConstants.K_ConnectionString);
        }

        public static SqlCeConnection GetConnection(string connString)
        {
            //Created by: VMCoder
            //Creation date: 2008.04.21
            //Last updated by: VMCoder
            //Last update: 2008.04.21

            if (connString.IndexOf("Data Source") < 0)
                connString = "Data Source = " + connString + ";";

            SqlCeConnection _Con = new SqlCeConnection(connString);
            
            if (_Con.State != ConnectionState.Open)
                _Con.Open();

            return _Con;
        }
        
        /// <summary>
        /// Creates and returns a sql parameter
        /// </summary>
        public static SqlCeParameter CreateParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10
            
            return new SqlCeParameter(parameterName, dbType, size, direction, isNullable, precision, scale, String.Empty, DataRowVersion.Default, value);
        }
        /// <summary>
        /// Creates and returns a sql Int parameter
        /// </summary>
        
        public static SqlCeParameter CreateIntParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.Int, 0, direction, isNullable, 0, 0, value);
        }
        /// <summary>
        /// Creates and returns a sql Bit parameter
        /// </summary>
        
        public static SqlCeParameter CreateBitParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.Bit, 0, direction, isNullable, 0, 0, value);
        }
        /// <summary>
        /// Creates and returns a sql DateTime parameter
        /// </summary>
        
        public static SqlCeParameter CreateDateTimeParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.DateTime, 0, direction, isNullable, 0, 0, value);
        }
        
        /// <summary>
        /// Creates and returns a sql NVarChar parameter
        /// </summary>
        public static SqlCeParameter CreateNVarCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.NVarChar, size, direction, isNullable, 0, 0, value);
        }
        
        /// <summary>
        /// Creates and returns a sql Decimal parameter
        /// </summary>
        public static SqlCeParameter CreateDecimalParameter(string parameterName, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.Decimal, 0, direction, isNullable, precision, scale, value);
        }

        #endregion
    }
}
